package ioc

import (
	aiv1 "gitee.com/flycash/ai-gateway-demo/api/proto/gen/ai/gateway/v1"
	aigrpc "gitee.com/flycash/ai-gateway-demo/internal/grpc"
	"gitee.com/flycash/ai-gateway-demo/internal/grpc/interceptor/jwt"
	"gitee.com/flycash/ai-gateway-demo/internal/grpc/interceptor/rate_limiter"
	"gitee.com/flycash/ai-gateway-demo/internal/service/llm"
	"github.com/gotomicro/ego/server/egrpc"
	"google.golang.org/grpc"
)

func InitGrpc(service llm.Service, jwtAuth *jwt.JwtAuth,tb *rate_limiter.TokenBucket) *egrpc.Component {
	jwtInterceptor := jwt.JwtAuthInterceptor(jwtAuth)

	rateLimitInterceptor := rate_limiter.UnaryServerInterceptor(tb)
	server := egrpc.Load("grpc").Build(egrpc.WithServerOption(grpc.ChainUnaryInterceptor(jwtInterceptor,rateLimitInterceptor)))
	grpcServer := aigrpc.NewLLMServiceServer(service)
	aiv1.RegisterAIGatewayServer(server, grpcServer)
	return server
}
